from django.db import models
from django.utils import timezone

class PointRecord(models.Model):
    """积分历史记录表"""
    POINT_TYPE_CHOICES = (
        (1, '购物奖励'),
        (2, '活动奖励'),
        (3, '签到奖励'),
        (4, '商品兑换'),
        (6, '其他')
    )
    
    record_id = models.BigAutoField(primary_key=True)
    user_id = models.BigIntegerField()
    mq_points = models.IntegerField(help_text='积分变动数量，正数为增加，负数为消费')
    type = models.SmallIntegerField(choices=POINT_TYPE_CHOICES)
    reference_id = models.BigIntegerField(null=True, blank=True, help_text='关联ID，如订单ID、优惠券ID等')
    description = models.CharField(max_length=255, null=True, blank=True)
    create_time = models.DateTimeField(default=timezone.now)
    
    class Meta:
        managed = False
        db_table = 'tb_point_record'
        ordering = ['-create_time']
        verbose_name = '积分记录'
        verbose_name_plural = '积分记录'

class RewardItem(models.Model):
    """积分兑换商品表"""
    STATUS_CHOICES = (
        (1, '上架'),
        (0, '下架')
    )
    REWARD_TYPE_CHOICES = (
        (2, '实物奖品'),
        (3, '妙趣徽章')
    )
    
    reward_id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=100)
    description = models.TextField(null=True, blank=True)
    points_required = models.IntegerField()
    reward_type = models.SmallIntegerField(choices=REWARD_TYPE_CHOICES)
    related_id = models.BigIntegerField(null=True, blank=True, help_text='关联ID，如优惠券ID')
    stock = models.IntegerField(default=0)
    image_url = models.CharField(max_length=255, null=True, blank=True)
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=1)
    create_time = models.DateTimeField(default=timezone.now)
    
    class Meta:
        managed = False
        db_table = 'tb_reward_item'
        verbose_name = '积分兑换商品'
        verbose_name_plural = '积分兑换商品'

class PointsExchange(models.Model):
    """积分兑换记录表"""
    STATUS_CHOICES = (
        (1, '待处理'),
        (2, '已完成'),
        (3, '已取消')
    )
    
    exchange_id = models.BigAutoField(primary_key=True)
    user_id = models.BigIntegerField()
    reward_id = models.BigIntegerField()
    points_used = models.IntegerField()
    exchange_time = models.DateTimeField(default=timezone.now)
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=1)
    receive_info = models.TextField(null=True, blank=True, help_text='收货信息JSON')
    
    class Meta:
        managed = False
        db_table = 'tb_points_exchange'
        ordering = ['-exchange_time']
        verbose_name = '积分兑换记录'
        verbose_name_plural = '积分兑换记录'
